<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品类型" prop="productTypeCode">
        <el-select
          v-model="queryParams.productTypeCode"
          placeholder="产品类型"
          clearable
          filterable
          style="width: 240px"
          @change="queryProductTypeChange"
          >
          <el-option
            v-for="item in productType_list"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="产品型号" prop="productCode">
        <el-select
          v-model="queryParams.productCode"
          placeholder="产品型号"
          clearable
          filterable
          style="width: 240px"
          >
          <el-option
            v-for="item in query_productCode_list"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出厂总数" prop="outFactoryTotal">
        <el-input
          v-model="queryParams.outFactoryTotal"
          placeholder="请输入出厂总数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打印类型" prop="printType">
        <el-select
          v-model="queryParams.printType"
          placeholder="打印类型"
          clearable
          filterable
          style="width: 240px"
          >
          <el-option
              v-for="dict in dict.type.print_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="计划出厂日期" prop="planOutFactoryDate" label-width="110px">
        <el-date-picker clearable
          v-model="queryParams.planOutFactoryDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择计划出厂日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计划出厂日期出厂数" prop="planOutFactoryCount" label-width="140px">
        <el-input
          v-model="queryParams.planOutFactoryCount"
          placeholder="请输入计划出厂日期出厂数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['produce:produce-out-factory-plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['produce:produce-out-factory-plan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['produce:produce-out-factory-plan:removed']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['produce:produce-out-factory-plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ProduceOutFactoryPlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="产品型号" align="center" prop="productCode" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="出厂总数" align="center" prop="outFactoryTotal" />
      <el-table-column label="计划出厂日期" align="center" prop="planOutFactoryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planOutFactoryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划出厂日期出厂数" align="center" prop="planOutFactoryCount" />
      <el-table-column label="打印类型" align="center" prop="printType">
        <template slot-scope="scope">
          {{ scope.row.printType | formatPrintTypeClass }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['produce:produce-out-factory-plan:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['produce:produce-out-factory-plan:removed']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出厂计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="产品类型" prop="productTypeCode">
            <el-select
              v-model="form.productTypeCode"
              placeholder="产品类型"
              clearable
              filterable
              style="width: 240px"
              @change="handleProductTypeChange"
            >
              <el-option
                v-for="item in productType_list"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="产品型号" prop="productCode">
            <!-- <el-input v-model="form.productTypeCode" placeholder="请输入产品类型代码" /> -->
              <el-select
              v-model="form.productCode"
              placeholder="请选择产品型号"
              clearable
              filterable
              style="width: 240px"
            >
              <el-option
                v-for="item in productCode_list"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="打印类型" prop="printType">
            <el-select
              v-model="form.printType"
              placeholder="打印类型"
              clearable
              filterable
              style="width: 240px"
              >
              <el-option
                  v-for="dict in dict.type.print_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="出厂总数" prop="outFactoryTotal">
          <el-input v-model="form.outFactoryTotal" placeholder="请输入出厂总数" />
        </el-form-item>
        <el-form-item label="计划出厂日期" prop="planOutFactoryDate">
          <el-date-picker clearable
            v-model="form.planOutFactoryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划出厂日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计划出厂日期出厂数" prop="planOutFactoryCount">
          <el-input v-model="form.planOutFactoryCount" placeholder="请输入计划出厂日期出厂数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduceOutFactoryPlan, getProduceOutFactoryPlan, removedProduceOutFactoryPlan, addProduceOutFactoryPlan, updateProduceOutFactoryPlan } from "@/api/produce/produce-out-factory-plan";
import { listProductInfo } from "@/api/produce/product-info";
import { listProductType } from "@/api/produce/product-type";

export default {
  name: "Produce-out-factory-plan",
  dicts:['print_type'],
  data() {
    return {
      // 请求的产品型号列表
      query_productCode_list:[],
      // 产品代码列表
      productCode_list:[],
      // 产品类型列表
      productType_list:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出厂计划表格数据
      ProduceOutFactoryPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productTypeCode: null,
        productCode: null,
        customerName: null,
        outFactoryTotal: null,
        planOutFactoryDate: null,
        planOutFactoryCount: null,
        removed: null,
        orderByColumn:'id',
          isAsc:'desc'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productCode: [
          { required: true, message: "产品型号不能为空", trigger: "blur" }
        ],
        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        outFactoryTotal: [
          { required: true, message: "出厂总数不能为空", trigger: "blur" }
        ],
        planOutFactoryDate: [
          { required: true, message: "计划出厂日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getProductType();
  },
  filters: {
    formatPrintTypeClass(key) {
      const map = {
        0 : '设备编码',
        1 : 'IMEI',
        2 : 'CPUID',
        3 : 'MAC',
        4 : 'SN'
      }
      return map[key]
    },
  },
  methods: {
    /** 获取产品类型列表 */
    getProductType () {
      listProductType(this.productTypeQueryParams).then(response => {
        this.productType_list = response.rows;
        // 下拉框赋默认值
        // this.queryParams.productTypeCode = this.productTypeList[0].code;
        // 再去查询列表
        this.getList();
      });
    },
    /** 点击检索产品类型列表变化 */
    queryProductTypeChange(){
      if(!this.queryParams.productTypeCode){
        return
      }
      const params={
        pageNum: 1,
        pageSize: 1000,
        productTypeCode: this.queryParams.productTypeCode
      }
      listProductInfo(params).then(response => {
        for(var i=0 ; i<response.rows.length ; i++){
          let temp = {
            code:response.rows[i].productCode,
            name:response.rows[i].productCode
          }
          this.query_productCode_list.push(temp);
        }
      }).catch(error => {
        this.$message.error("查询产品代码列表失败",error.msg);
      }).finally(() =>{
        this.$forceUpdate();
      })
    },
    /** 点击产品类型列表变化 */
    handleProductTypeChange(){
      const params={
        pageNum: 1,
        pageSize: 1000,
        productTypeCode: this.form.productTypeCode
      }
      listProductInfo(params).then(response => {
        for(var i=0 ; i<response.rows.length ; i++){
          let temp = {
            value:response.rows[i].productCode,
            label:response.rows[i].productCode
          }
          this.productCode_list.push(temp);
        }
      }).catch(error => {
        this.$message.error("查询产品代码列表失败",error.msg);
      }).finally(() =>{
        this.$forceUpdate();
      })
    },
    /** 查询出厂计划列表 */
    getList() {
      console.log(this.queryParams4)
      if(this.queryParams.productTypeCode){
        if(!this.queryParams.productCode){
          this.$message.warning("仅选择产品类型无法进行有效检索，请选择一款产品型号");
          return
        }
      }
      this.loading = true;
      listProduceOutFactoryPlan(this.queryParams).then(response => {
        this.ProduceOutFactoryPlanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productCode: null,
        customerName: null,
        outFactoryTotal: null,
        planOutFactoryDate: null,
        planOutFactoryCount: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        removed: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出厂计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduceOutFactoryPlan(id).then(response => {
        this.form = response.data;
        let productCode = response.data.productCode;
        this.handleProductTypeChange();
        this.form.productCode = productCode;
        this.open = true;
        this.title = "修改出厂计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduceOutFactoryPlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduceOutFactoryPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除出厂计划编号为"' + ids + '"的数据项？').then(function() {
          // 修改为逻辑删除
        return removedProduceOutFactoryPlan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('produce/produce-out-factory-plan/export', {
        ...this.queryParams
      }, `produce-out-factory-plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
